<?php

class Controller extends Yaf_Controller_Abstract{

    //配置文件
    protected $_config;

    //Session
    protected $_session;

    /**
     * @var Db_MySQLi
     */
    protected $_db_handler;

    /**
     * @var Cache_Memcache
     */
    protected $_cache_handler;

    protected $_authManager;

    protected $_AppID ="wxacd3c21e4f1582ed";
    protected $_AppSecret ="09ea752e7acc74e358b5d78886af7343";
    protected $_JsonAdr = "/public/cache/json/";


    public function init(){
        $this->_config = Yaf_Registry::get("config");
        $this->_session = Yaf_Session::getInstance();
        $this->_session->start();

        //系统功能初始化
        $this->_db_handler = Yaf_Registry::get("db_handler");
        $this->_cache_handler = Yaf_Registry::get("cache_MemCache");
        $user_info = $this->_session->offsetGet('loginInfos');
        $this->_view->user_info =$user_info;
//        $this->writeLog($user_info);
        //获取地址
        $_SERVER['REQUEST_URI'] = trim($_SERVER['REQUEST_URI'], '/ ');
        $args   = explode('/', $_SERVER['REQUEST_URI']);

        if(empty($args[0])){
            $args[0]  = 'index';
        }else{
            if($args[0]=='user'){
               if(empty($args[1])){
                   $args[1]='index';
               }
            }
        }


        $this->_view->menu_title = $args[0];
        $this->_view->menu_title_tip = empty($args[1]) ? 'index' :$args[1];

//
//        $wxapi = new Jssdk($this->_AppID,$this->_AppSecret,$this->_JsonAdr);
//        $wxdata =  $wxapi->GetSignPackage();
//        $this->_view->wxdata = $wxdata;
    }
    //浏览网站记录
    private function writeLog($user_info){
        $com = new CommonModel();
        //获取当前的浏览地址
        //获取地址
        $_SERVER['REQUEST_URI'] = trim($_SERVER['REQUEST_URI'], '/ ');
        $args   = explode('/', $_SERVER['REQUEST_URI']);
        //这边指定操作
        // 1./index,,3./principal,4./supply,,7.news,,9/shopcart,10./user
        $url_mark = array(
            array(
                'method'=>'/index',
                'remark'=>'首页',
            ),
            array(
                'method'=>'/principal',
                'remark'=>'现货资源列表',
            ),
            array(
                'method'=>'/buy',
                'remark'=>'求购列表',
            ),
            array(
                'method'=>'/supply',
                'remark'=>'供应商列表',
            ),
            array(
                'method'=>'/shopcart',
                'remark'=>'购物车',
            ),
            array(
                'method'=>'/user',
                'remark'=>'会员中心',
            ),
            array(
                'method'=>'/news',
                'remark'=>'新闻资讯',
            ),
        );

        if(!empty($user_info)){
            //只记录登录的用户操作
            foreach($url_mark as $key=>$var){
                if(strpos($args[0],trim($var['method'],"/")) !==false){
                    //这边说明执行了上面的操作
                    $data = array(
                        'create_id'=>$user_info['id'],
                        'create_time'=>date("Y-m-d H:i:s",strtotime('now')),
                        'user_id'=>$user_info['id'],
                        'request_url'=>urldecode($_SERVER['REQUEST_URI']),
                        'remark'=>$var['remark'],
                        'ip'=>Tools::getRemoteAddr(),
                    );
                    $com->addSiteLog($data);
                }
            }

        }

    }

}
